//@version=6
indicator("Stochastic + RSI in one", overlay=false)


//====================================================
// INPUTS
//====================================================

// Stochastic MT5 style
kPeriod = input.int(5, "Stochastic K Period")
dPeriod = input.int(3, "Stochastic D Period")
slowing = input.int(3, "Stochastic Slowing")

// RSI
rsiPeriod = input.int(14, "RSI Period")

// Extreme zones
stochHigh = input.int(85, "SELL Stochastic Level")
stochLow  = input.int(15, "BUY Stochastic Level")

rsiHigh = input.int(70, "RSI SELL Level")
rsiLow  = input.int(30, "RSI BUY Level")


//====================================================
// STOCHASTIC MT5 STYLE
//====================================================

lowestLow = ta.lowest(low, kPeriod)
highestHigh = ta.highest(high, kPeriod)

rangeValue = highestHigh - lowestLow

rawK = rangeValue != 0 ? ((close - lowestLow) / rangeValue) * 100 : 100

// Slowing
stochK = ta.sma(rawK, slowing)

// Signal line D
stochD = ta.sma(stochK, dPeriod)



//====================================================
// RSI WILDER (MT5 STYLE)
//====================================================

change = ta.change(close)

gain = math.max(change, 0)
loss = math.max(-change, 0)

avgGain = ta.rma(gain, rsiPeriod)
avgLoss = ta.rma(loss, rsiPeriod)

rs = avgLoss != 0 ? avgGain / avgLoss : 0

rsi = avgLoss != 0 ? 100 - (100 / (1 + rs)) : 100



//====================================================
// SIGNAL CONDITIONS
//====================================================

stochBullCross = ta.crossover(stochK, stochD)
stochBearCross = ta.crossunder(stochK, stochD)


// BUY:
// Stochastic wychodzi z ekstremalnego dołka
// RSI jest wyprzedany

buySignal =
     stochBullCross and
     stochK < stochLow and
     rsi < rsiLow



// SELL:
// Stochastic zawraca z ekstremalnej góry
// RSI jest wykupiony

sellSignal =
     stochBearCross and
     stochK > stochHigh and
     rsi > rsiHigh



//====================================================
// COLORS
//====================================================

// Linie oscylatorów - łagodne
softAqua = color.rgb(120, 220, 220)
softRed = color.rgb(255, 140, 140)
softBlue = color.rgb(130, 170, 255)

// Strzałki - mocniejsze
strongGreenArrow = color.rgb(0, 200, 80)
strongRedArrow = color.rgb(230, 40, 40)



//====================================================
// PLOTS
//====================================================

plot(
     stochK,
     title="Stochastic K",
     color=softAqua,
     linewidth=2
)


plot(
     stochD,
     title="Stochastic D",
     color=softRed,
     linewidth=2
)


plot(
     rsi,
     title="RSI",
     color=softBlue,
     linewidth=2
)



//====================================================
// LEVELS
//====================================================

hline(85, "85 SELL zone", color=color.gray)
hline(70, "70 RSI zone", color=color.gray)
hline(50, "50", color=color.gray)
hline(30, "30 RSI zone", color=color.gray)
hline(15, "15 BUY zone", color=color.gray)



